'''
Created on Nov 4, 2011

@author: onetera
'''
from PyQt4 import QtCore , QtGui
from lib.papyDB import papyDB , tableInfo
from lib.papyDBTable import *
from lib.itemModel import *
from lib import globalVar as GV
import os


class customUI(QtGui.QMainWindow): 
    
    def __init__(self):
        self.sizePolicy_fxfx = QtGui.QSizePolicy(QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Fixed)
        self.sizePolicy_fxpr = QtGui.QSizePolicy(QtGui.QSizePolicy.Fixed,  QtGui.QSizePolicy.Preferred)
        self.sizePolicy_mnfx = QtGui.QSizePolicy(QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Fixed)
        self.sizePolicy_mnmn = QtGui.QSizePolicy(QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Minimum)
        self.sizePolicy_prmx = QtGui.QSizePolicy(QtGui.QSizePolicy.Preferred, QtGui.QSizePolicy.Maximum)
        self.sizePolicy_mnpr = QtGui.QSizePolicy(QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Preferred )
        self.sizePolicy_prpr = QtGui.QSizePolicy(QtGui.QSizePolicy.Preferred,  QtGui.QSizePolicy.Preferred )
        self.sizePolicy_expr = QtGui.QSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Preferred )
        self.sizePolicy_exex = QtGui.QSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding )
        
        self.mainFont = QtGui.QFont('Arial',10)
        self.mainFont.setFamily("Arial,dotum"  )
        self.mainFont.setPointSize( 10 ) 

        self.txtEdtFont = QtGui.QFont('Arial',12)
        self.txtEdtFont.setFamily("Arial ,dotum"  )
        self.txtEdtFont.setPointSize(12) 
        
        self.KORFont = QtGui.QFont('dotum',10)
#        self.mainFont.family( 'Arial'  )
#        self.mainFont.setPointSize(10)
#        QPushButton{ background-color: #dadee9; color: #252a36; font-weight:bold;}
        self.styleSheet = '''        
        QGroupBox{ color: #252a36; }
        QComboBox {  color : #000000 }
        QLineEdit { color : #000000 ; }
        QLineEdit[mandatory = "true"]{
            background-color: rgb(255,255,127);
            color : darkblue;
            }
        '''
        self.singleComm = 0 
        
             
             
    def grpBox(self , name , sizePolicy):
        theGrpBox = QtGui.QGroupBox(name)
        theGrpBox.setSizePolicy(sizePolicy)
        theGrpBox.setMinimumSize(QtCore.QSize(150, 60))
        theGrpBox.setFont(self.mainFont)                
        return theGrpBox
    
    
    
    def lineEdt(self):
        lineEdit = QtGui.QLineEdit()
        lineEdit.setMinimumSize(QtCore.QSize(150, 25))
        lineEdit.setFont(self.mainFont)        
        return lineEdit
        
        
        
    def CBox(self ,  sizePolicy):
        theCBox = QtGui.QComboBox()
        theCBox.setSizePolicy(sizePolicy)
        theCBox.setMinimumSize(QtCore.QSize(130, 25))
        theCBox.setFont(self.mainFont)
        theCBox.currentIndex()    
        return theCBox
    
    
    
    def grpCBox(self , name):
        grp = self.grpBox(name, self.sizePolicy_fxfx )        
        theGrpCBox = self.CBox(self.sizePolicy_fxfx)        
        layout = QtGui.QGridLayout()
        layout.addWidget(theGrpCBox, 0 , 0)
        grp.setLayout(layout)
        return grp,theGrpCBox
    
    
    
    def grpLineEdt(self , name ):
        grp =  self.grpBox( name , self.sizePolicy_mnfx)
        lineEdt = self.lineEdt()
        lineEdt.setFont( self.KORFont )        
        layout = QtGui.QGridLayout()
        layout.addWidget(lineEdt , 0 , 0)
        grp.setLayout(layout)
        return grp , lineEdt
    
    
    
    def cancelDoBtn(self , do, cancel="Cancel" ):
        cancel_btn =  self.btn( cancel )        
        do_btn =  self.btn( do )
        cancel_btn.setMinimumSize(40, 35)
        do_btn.setMinimumSize(40, 35)
#        cancel_btn.clicked.connect(QtCore.QCoreApplication.instance().quit)
        return do_btn , cancel_btn
    
    
    
    def txtEditor(self):
        txtEdt = QtGui.QTextEdit() 
        txtEdt.setFont(self.KORFont)        
        txtEdt.setMinimumHeight(60)
        return txtEdt  
    
    
        
    def btn(self,name):
        button = QtGui.QPushButton(name)
        button.setFont(self.mainFont)
        return button 
        
        
        
    def chkBox(self, name,parent = None):
        chk = QtGui.QCheckBox(parent)
        chk.setText( name )
        chk.setChecked(True)
        chk.setSizePolicy(self.sizePolicy_prmx)
        chk.setFont(self.mainFont)
        chk.setMinimumSize(QtCore.QSize(150, 25))
        return chk
        
        
        
    def dateBox(self,name , btnName):
        grp = self.grpBox( name , self.sizePolicy_fxfx)
        lineEdit = self.lineEdt()        
        button = self.btn(btnName)
        for x in [grp,lineEdit,button]:
            x.setSizePolicy(QtGui.QSizePolicy.Preferred, QtGui.QSizePolicy.Fixed)
        layout = QtGui.QGridLayout()
        layout.addWidget(lineEdit, 0 , 0)
        layout.addWidget(button, 0 , 1)
        grp.setLayout(layout)
        return grp , lineEdit , button
    
    
    
    def label(self , name):
        lb = QtGui.QLabel(name)
        lb.setFont(self.mainFont)        
        return lb
    
    

    def calendar(self , name):
        theDiag = QtGui.QDialog(self.mainWidget )
        theDiag.setWindowTitle(name)
        theDiag.resize(260,200)
        self.cal = QtGui.QCalendarWidget(theDiag)
        self.cal.setGridVisible(True)
        self.cal.setFont(self.mainFont)
        layout = QtGui.QGridLayout()
        layout.addWidget(self.cal, 0 , 0)
        theDiag.setLayout(layout)
        cursor = QtGui.QCursor()
        theDiag.setGeometry( QtCore.QRect( cursor.pos().x()-5, cursor.pos().y()+20,260,200 ) )     
        theDiag.show()
        return self.cal
    
    
    
    def calendar2(self):
        cal = QtGui.QCalendarWidget()
        cursor = QtGui.QCursor()
        cal.setGeometry( QtCore.QRect( cursor.pos().x()-5, cursor.pos().y()+20,260,200 ) )
        cal.setGridVisible(True)
        cal.setFont( self.mainFont )        
#        cal.show()
        return cal
    
    
    
    def calendar3(self  ):
        theDiag = QtGui.QDialog( )
#        theDiag.setWindowTitle(name)
        theDiag.resize(260,200)
        self.cal = QtGui.QCalendarWidget(theDiag)
        self.cal.setGridVisible(True)
        self.cal.setFont(self.mainFont)
        layout = QtGui.QGridLayout()
        layout.addWidget(self.cal, 0 , 0)
        theDiag.setLayout(layout)
        cursor = QtGui.QCursor()
        theDiag.setGeometry( QtCore.QRect( cursor.pos().x()-5, cursor.pos().y()+20,260,200 ) )     
        theDiag.show()
        return self.cal
    
    
         
    def createPSSADcb(self):
        self.prj_grp , self.prj_cb = self.grpCBox('Project')
        self.seq_grp , self.seq_cb = self.grpCBox('Sequence')
        self.shot_grp , self.shot_cb = self.grpCBox('Shot')
        self.asset_grp , self.asset_cb = self.grpCBox('Asset')
        self.dept_grp , self.dept_cb = self.grpCBox('Depart')
        self.PSSAD_layout = QtGui.QHBoxLayout()
        self.PSSAD_layout.addWidget(self.prj_grp)
        self.PSSAD_layout.addWidget(self.seq_grp)
        self.PSSAD_layout.addWidget(self.shot_grp)
        self.PSSAD_layout.addWidget(self.asset_grp)
        self.PSSAD_layout.addWidget(self.dept_grp)
        


    def createPSSDcb(self):
        self.prj_grp , self.prj_cb = self.grpCBox('Project')
        self.seq_grp , self.seq_cb = self.grpCBox('Sequence')
        self.shot_grp , self.shot_cb = self.grpCBox('Shot')
        self.dept_grp , self.dept_cb = self.grpCBox('Depart')
        self.PSSD_layout = QtGui.QHBoxLayout()
        self.PSSD_layout.addWidget(self.prj_grp)
        self.PSSD_layout.addWidget(self.seq_grp)
        self.PSSD_layout.addWidget(self.shot_grp)
        self.PSSD_layout.addWidget(self.dept_grp)
        

    def createPSScb(self):
        self.prj_grp , self.prj_cb = self.grpCBox('Project')
        self.seq_grp , self.seq_cb = self.grpCBox('Sequence')
        self.shot_grp , self.shot_cb = self.grpCBox('Shot')
        self.PSS_layout = QtGui.QHBoxLayout()
        self.PSS_layout.addWidget(self.prj_grp)
        self.PSS_layout.addWidget(self.seq_grp)
        self.PSS_layout.addWidget(self.shot_grp)
            
                
    def initDept(self):
        self.dept_cb.clear()
        deptList = papyDB.getFetch('select deptName from DEPARTMENT')
#        deptList = []
        map(lambda x : self.dept_cb.addItem(x[0]) , deptList )
  
        
            
    def initPrj(self):
        prjList = papyDB.getFetch('select prjCodeName from PROJECT')
        map(lambda x : self.prj_cb.addItem(x[0]) , prjList )
                
                
                
    def initSeq(self , master=''):        
        self.seq_cb.clear()         
        query = '''
        select seqName from SEQUENCES,PROJECT  
        where SEQUENCES.prjID = PROJECT.prjID and prjCodeName = '%s' 
        ''' % str( self.prj_cb.currentText() )   
        seqList = papyDB.getFetch( query )               
        map( lambda x:self.seq_cb.addItem(x[0]) , seqList )
        if master != '':
            self.seq_cb.insertItem(0,'--Master--')        
        
        
        
    def initShot(self , iglobal = ''):            
        self.shot_cb.clear()        
        query = '''
        select shotName from SHOT,SEQUENCES 
        where SHOT.seqID = SEQUENCES.seqID and seqName = '%s'  
        ''' % str( self.seq_cb.currentText() )
        shotList = papyDB.getFetch( query )        
        map( lambda x:self.shot_cb.addItem(x[0]) , shotList )
        if iglobal != '':
            self.shot_cb.insertItem(0,'--Global--')
            
            
    def initAsset_shotAssem(self , iSelect=''):
        self.asset_cb.clear()
        
        query = '''
        select assetName
        from ASSET_JOB , ASSET , SHOT_has_ASSET
        where ASSET_JOB.assetID = ASSET.assetID AND assetName = '%s'
        ''' % str(self.shot_cb.currentText()) 

        assetList = papyDB.getFetch( query )        
        map(lambda x:self.asset_cb.addItem(x[0]) , assetList )
        if iSelect != '':
            self.asset_cb.insertItem(0,'--------')
            
                    
    def initAsset(self , iSelect=''):
#        self.userID = papyDB.getFetch('select userID from USER_INFO where userLogInName = "%s"' % os.environ["username"])[0][0]
        
        if GV.testUserID == '': self.userID = USER_INFO.search('userID' , userLogInName = os.environ['username'])
        else :                  self.userID = GV.testUserID
        
        
        self.asset_cb.clear()
        if( str( self.seq_cb.currentText() ) == "--Master--") : 
            query = '''
            select ASSET.assetName from ASSET, PROJECT_has_ASSET
            where PROJECT_has_ASSET.assetID = ASSET.assetID AND PROJECT_has_ASSET.prjID = '%s'
            ''' % ( papyDB.getID("PROJECT", "prjID", "prjCodeName", str( self.prj_cb.currentText() ) ) )    
        elif( str( self.shot_cb.currentText() ) == "--Global--") :
            query = '''
            select ASSET.assetName from ASSET, SEQUENCES_has_ASSET
            where SEQUENCES_has_ASSET.assetID = ASSET.assetID AND SEQUENCES_has_ASSET.seqID = '%s'
            ''' % ( papyDB.getID("SEQUENCES", "seqID", "seqName", str( self.seq_cb.currentText() ) ) )
        else :
            query = '''
            select A.assetName from ASSET as A, SHOT_has_ASSET as SA
            where SA.assetID = A.assetID AND SA.shotID = '%s'
            ''' % ( papyDB.getID("SHOT", "shotID", "shotName", str( self.shot_cb.currentText() ) ) )    
        assetList = papyDB.getFetch( query )

        map(lambda x:self.asset_cb.addItem(x[0]) , assetList)
        if iSelect != '':
            self.asset_cb.insertItem(0 , '--------')
           

 
    def initAsset_assetPublish(self , iSelect=''):
        self.asset_cb.clear()
######################################################################################################################        
        if( str( self.seq_cb.currentText() ) == "--Master--") : 
            query = '''
            select ASSET.assetID from ASSET, PROJECT_has_ASSET
            where PROJECT_has_ASSET.assetID = ASSET.assetID AND PROJECT_has_ASSET.prjID = '%s'
            ''' % ( papyDB.getID("PROJECT", "prjID", "prjCodeName", str( self.prj_cb.currentText() ) ) )    
        elif( str( self.shot_cb.currentText() ) == "--Global--") :
            query = '''
            select ASSET.assetID from ASSET, SEQUENCES_has_ASSET
            where SEQUENCES_has_ASSET.assetID = ASSET.assetID AND SEQUENCES_has_ASSET.seqID = '%s'
            ''' % ( papyDB.getID("SEQUENCES", "seqID", "seqName", str( self.seq_cb.currentText() ) ) )
        else :
            query = '''
            select A.assetID from ASSET as A, SHOT_has_ASSET as SA
            where SA.assetID = A.assetID AND SA.shotID = '%s'
            ''' % ( papyDB.getID("SHOT", "shotID", "shotName", str( self.shot_cb.currentText() ) ) )    
        PSSAssetTuple = papyDB.getFetch( query )
        PSSAssetList = []
        for x in PSSAssetTuple :
            PSSAssetList.append(   str(x[0])   )
######################################################################################################################
#        self.userID = papyDB.getFetch('select userID from USER_INFO where userLogInName = "%s"' % os.environ["username"])[0][0]
        if GV.testUserID == '': self.userID = USER_INFO.search('userID' , userLogInName = os.environ['username'])
        else :                  self.userID = GV.testUserID
        query2 = ''' select assetID from ASSET_JOB where userID = '%s' ''' % self.userID
        userAssetTuple = papyDB.getFetch( query2 )
        userAssetList = []
        for x in userAssetTuple :
            userAssetList.append(   str(x[0])   )
######################################################################################################################
#        print "===================================================================================="
#        print "PSSAssetList = " , PSSAssetList
#        print "userAssetList = " , userAssetList
        assetList = []        
        for x in PSSAssetList : 
            if x in userAssetList :
                query3 = ''' select assetName from ASSET where assetID = '%s' ''' % x
                asset = papyDB.getFetch( query3 )
#                print asset[0][0]
                assetList.append( papyDB.getFetch(query3)[0][0] )
#        print "assetList = " , assetList
######################################################################################################################   
        map(lambda x:self.asset_cb.addItem(x) , assetList)
        if iSelect != '':
            self.asset_cb.insertItem(0 , '--------')
            
            
                        
    def alertWin(self , title , commt):
        msgBox = QtGui.QMessageBox( self )
        font = QtGui.QFont('Arial',15)
        msgBox.setFont(font)        
        msgBox.setWindowTitle(title)
        msgBox.setFixedSize(300,100)
        msgBox.setText( commt )
        msgBox.show()
        
        
        
    def getDate(self, cal):
        return cal.selectedDate()    
    
    
    
    def setToLineEdt(self,edt):
        date = self.getDate(self.cal)             
        edt.setText( str(date.year()) +"-"+ str(date.month()) +"-"+ str(date.day())  )
        